package com.wzb.mybatisplus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzb.mybatisplus.bitaction.ArchiveThreadPool;
import com.wzb.mybatisplus.bitaction.BitcoinPrivateKeyGenerator1;
import com.wzb.mybatisplus.bitaction.BitcoinWIFConverter;
import com.wzb.mybatisplus.bitaction.electrumaction.FileUtils;
import com.wzb.mybatisplus.bitaction.electrumaction.one.BitcoinKeyConverter;
import com.wzb.mybatisplus.bitaction.electrumaction.one.BlockchainBalanceChecker;
import com.wzb.mybatisplus.bitaction.electrumaction.one.KeyPair;
import com.wzb.mybatisplus.bitaction.electrumaction.three.SendEmailToQQ;
import com.wzb.mybatisplus.bitaction.formal.BlockCypherBatchBalance;
import com.wzb.mybatisplus.bitaction.model.Blockcypher;
import com.wzb.mybatisplus.bitaction.model.GetManeyStrategy;
import com.wzb.mybatisplus.bitaction.section.GetSection;
import com.wzb.mybatisplus.bitaction.section.GetSection1;
import com.wzb.mybatisplus.entity.Bituse;
import com.wzb.mybatisplus.entity.Sy;
import com.wzb.mybatisplus.entity.Syone;
import com.wzb.mybatisplus.mapper.BituseMapper;
import com.wzb.mybatisplus.mapper.SyMapper;
import com.wzb.mybatisplus.mapper.SyoneMapper;
import com.wzb.mybatisplus.service.IBituseService;
import lombok.extern.slf4j.Slf4j;
import org.bitcoinj.core.*;
import org.bitcoinj.params.MainNetParams;
import org.bitcoinj.script.Script;
import org.bitcoinj.script.ScriptBuilder;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.*;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;
@Slf4j
@Service
public class BituseServiceImpl extends ServiceImpl<BituseMapper, Bituse> implements IBituseService {
    public static final String ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
    private static final NetworkParameters MAINNET = MainNetParams.get();

    @Autowired
    private SyMapper syMapper ;

    @Autowired
    private SyoneMapper syoneMapper;

    private List<Syone> findSyOne;
    public  void aa (){



        int sum =0;
        for(int a1=1 ; a1<=1;a1++)
        for(int a2=0 ; a2<ALPHABET.length();a2++)
        for(int a3=0 ; a3<ALPHABET.length();a3++)
        for(int a4=0 ; a4<ALPHABET.length();a4++)
        for(int a5=0 ; a5<ALPHABET.length();a5++)
        for(int a6=0 ; a6<ALPHABET.length();a6++)
        for(int a7=0 ; a7<ALPHABET.length();a7++)
        for(int a8=0 ; a8<ALPHABET.length();a8++)
        for(int a9=0 ; a9<ALPHABET.length();a9++)
        for(int a10=0 ; a10<ALPHABET.length();a10++)
        for(int a11=0 ; a11<ALPHABET.length();a11++)
        for(int a12=0 ; a12<ALPHABET.length();a12++)
        for(int a13=0 ; a13<ALPHABET.length();a13++)
        for(int a14=0 ; a14<ALPHABET.length();a14++)
        for(int a15=0 ; a15<ALPHABET.length();a15++)
        for(int a16=0 ; a16<ALPHABET.length();a16++)
        for(int a17=0 ; a17<ALPHABET.length();a17++)
        for(int a18=0 ; a18<ALPHABET.length();a18++)
         for(int a19=0 ; a19<ALPHABET.length();a19++)
       for(int a20=0 ; a20<ALPHABET.length();a20++)
       for(int a21=0 ; a21<ALPHABET.length();a21++)
       for(int a22=0 ; a22<ALPHABET.length();a22++)
       for(int a23=0 ; a23<ALPHABET.length();a23++)
       for(int a24=0 ; a24<ALPHABET.length();a24++)
       for(int a25=0 ; a25<ALPHABET.length();a25++)
        for(int a26=0 ; a26<ALPHABET.length();a26++)
       for(int a27=0 ; a27<ALPHABET.length();a27++)
       for(int a28=0 ; a28<ALPHABET.length();a28++)
       for(int a29=0 ; a29<ALPHABET.length();a29++)
       for(int a30=0 ; a30<ALPHABET.length();a30++)
        for(int a31=0 ; a31<ALPHABET.length();a31++)
        for(int a32=0 ; a32<ALPHABET.length();a32++)
        for(int a33=0 ; a33<ALPHABET.length();a33++)
       for(int a34=0 ; a34<ALPHABET.length();a34++)
       for(int a35=0 ; a35<ALPHABET.length();a35++)
       for(int a36=0 ; a36<ALPHABET.length();a36++)
       for(int a37=0 ; a37<ALPHABET.length();a37++)
       for(int a38=0 ; a38<ALPHABET.length();a38++)
       for(int a39=0 ; a39<ALPHABET.length();a39++)
       for(int a40=0 ; a40<ALPHABET.length();a40++)
       for(int a41=0 ; a41<ALPHABET.length();a41++)
       for(int a42=0 ; a42<ALPHABET.length();a42++)
       for(int a43=0 ; a43<ALPHABET.length();a43++)
       for(int a44=0 ; a44<ALPHABET.length();a44++)
       for(int a45=0 ; a45<ALPHABET.length();a45++)
       for(int a46=0 ; a46<ALPHABET.length();a46++)
       for(int a47=0 ; a47<ALPHABET.length();a47++)
       for(int a48=0 ; a48<ALPHABET.length();a48++)
       for(int a49=0 ; a49<ALPHABET.length();a49++)
       for(int a50=0 ; a50<ALPHABET.length();a50++)
       for(int a51=0 ; a51<ALPHABET.length();a51++){
          String maney = "L";

             maney= maney + ALPHABET.charAt(a1)+ALPHABET.charAt(a2)+ALPHABET.charAt(a3)+ALPHABET.charAt(a4)+ALPHABET.charAt(a5)
                +ALPHABET.charAt(a6)+ALPHABET.charAt(a7)+ALPHABET.charAt(a8)+ALPHABET.charAt(a9)+ALPHABET.charAt(a10)
                 +ALPHABET.charAt(a11)+ALPHABET.charAt(a12)+ALPHABET.charAt(a13)+ALPHABET.charAt(a14)+ALPHABET.charAt(a15)+ALPHABET.charAt(a16)
                  +ALPHABET.charAt(a17)+ALPHABET.charAt(a18)+ALPHABET.charAt(a19)+ALPHABET.charAt(a20)+ALPHABET.charAt(a21)+ALPHABET.charAt(a22)
                  +ALPHABET.charAt(a23)+ALPHABET.charAt(a24)+ALPHABET.charAt(a25)+ALPHABET.charAt(a26)+ALPHABET.charAt(a27)+ALPHABET.charAt(a28)+ALPHABET.charAt(a29)
                   +ALPHABET.charAt(a30)+ALPHABET.charAt(a31) +ALPHABET.charAt(a32)+ALPHABET.charAt(a33)+ALPHABET.charAt(a34)
                  +ALPHABET.charAt(a35)+ALPHABET.charAt(a36)+ALPHABET.charAt(a37)+ALPHABET.charAt(a38)+ALPHABET.charAt(a39)+ALPHABET.charAt(a40)
                   +ALPHABET.charAt(a41)+ALPHABET.charAt(a42)+ALPHABET.charAt(a43)+ALPHABET.charAt(a44)+ALPHABET.charAt(a45)+ALPHABET.charAt(a46)+
                   ALPHABET.charAt(a47)+ALPHABET.charAt(a48)+ALPHABET.charAt(a49)+ALPHABET.charAt(a50)+ALPHABET.charAt(a51);
                    System.out.println(maney);
                   String maney1 = maney;

           KeyPair wifKeyPair = null;
           try {
               wifKeyPair = BitcoinKeyConverter.generateFromWIF(maney1);
           } catch (Exception e) {
                continue;
           }
           String address = wifKeyPair.getLegacyAddress(); // 比特币创始区块地址公钥地址
           if(address.equals("1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa")){
               System.out.println("私钥"+maney1);
           }

//           ArchiveThreadPool.getThreadPoolInstance().execute(()->{
//               KeyPair wifKeyPair = null;
//               try {
//                   wifKeyPair = BitcoinKeyConverter.generateFromWIF(maney1);
//               } catch (Exception e) {
//
//               }
//
//               String address = wifKeyPair.getLegacyAddress(); // 比特币创始区块地址公钥地址
//
//
////               String a = BitcoinWIFConverter.wifTo34CharPublicKey(maney1);
//
//               if(address.equals("1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa")){
////                                sum=sum+1;
////                                System.out.println("============="+sum + "      秘钥" +maney1);
////               if(true){
//                   Bituse bituse = new Bituse();
//                   bituse.setWifsy(maney1);
//                   bituse.setGy("1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa");
//                   super.save(bituse);
//               }
//           });



        }



    }

    /**
     * 私钥L开头  1开头的是地址
     */
    @Override
    public void find1kaitou(String ad ,String add ,String path,int hao) {
//        chushihua();
        int conutsum =0;
        String pathSy = path+"\\Sy"+hao+".txt";
        String pathgy = path+"\\gy"+hao+".txt";
        String gyPath = "D:\\btc\\miyao\\find.txt";
        FileUtils fileUtils = new FileUtils();
        fileUtils.createFile(pathSy);
        fileUtils.createFile(pathgy);
        int j = 0;
//        String add =1+"";

//        String ad = "67999218802305177194863145464757464115623021623585035533703579771544440055647";
        Set<String> addresses =  new HashSet<>();
        Set<String> addressesSys = new HashSet<>();
        BigInteger a = new BigInteger(ad);
//        BitcoinPrivateKeyGenerator1.BitcoinPrivateKey privateKey1 = BitcoinPrivateKeyGenerator1.generatePrivateKey();
        BigInteger sy = a;
        int flag=0;
        for (int i = 0; i<1000000 ; i++){
            flag = 0;
            //随机生成私钥

            if(i>10000000){i=0;j++;}
//                BigInteger b = new BigInteger((i*10000)+"");

            BitcoinPrivateKeyGenerator1.BitcoinPrivateKey privateKey = BitcoinPrivateKeyGenerator1.generatePrivateKey(sy,new BigInteger(add));
            if(privateKey==null){flag=1;}
            if (privateKey!=null && BitcoinPrivateKeyGenerator1.isValidPrivateKey(privateKey.getPrivateKey())) {  //私钥正确的话

                // 私钥转公钥 k
                String wifPrivateKey = privateKey.getWIFCompressed();

                KeyPair wifKeyPair = null;
                try {
                    wifKeyPair = BitcoinKeyConverter.generateFromWIF(wifPrivateKey);
                } catch (Exception e) {

                }
                if(wifKeyPair==null){

                    sy = new BigInteger(privateKey.getHex(),16).add(new BigInteger(add));
                    continue;
                }
                String address = wifKeyPair.getLegacyAddress(); // 比特币创始区块地址公钥地址
                // 判断是否重复   不重复就加进来
                if(panduanChongfu(wifPrivateKey)) {
                    addresses.add(address);

                    addresses.add(wifKeyPair.getSegwitAddress());
                    addressesSys.add(wifPrivateKey);
                }
                ECKey ecKey = DumpedPrivateKey.fromBase58(MAINNET, wifPrivateKey).getKey();
                // Bech32 3开头)
                try {


                    Script p2wpkhScript = ScriptBuilder.createP2WPKHOutputScript(ecKey);
                    Script p2shScript = ScriptBuilder.createP2SHOutputScript(p2wpkhScript);
                    String p2sh = LegacyAddress.fromScriptHash(MAINNET, p2shScript.getPubKeyHash()).toString();  // 3开头
                    addresses.add(p2sh);


                }catch (Exception e){

                }


                NetworkParameters network = MainNetParams.get();
                DumpedPrivateKey dumpedPrivateKey = DumpedPrivateKey.fromBase58(network, privateKey.getWIFUncompressed());
                ECKey ecKey1 = dumpedPrivateKey.getKey();
                // 判断是否与数据库中重复
                if(panduanChongfu(privateKey.getWIFUncompressed())) {
                    // 生成地址
                    Address legacyAddress = LegacyAddress.fromKey(network, ecKey1);
                    addresses.add(legacyAddress.toString());
                    addressesSys.add(privateKey.getWIFUncompressed());
                }

                String publicKey = wifKeyPair.getPublicKeyCompressed();
                publicKey = publicKey.substring(2);
//                sy = clearHighestBit(sy);
                sy = sy.add(new BigInteger(add));
                if(sy.compareTo(new BigInteger("10000000"))<0){
                    flag = 1;
                }

            }else {
                sy = sy.add(new BigInteger(add));
            }
//                System.out.println(privateKey);

            if(addresses.size()>0&&(addresses.size()>=300 || flag==1 )) {
                String addressString = String.join("|", addresses);
                fileUtils.writeFile(addressString+"\n",pathgy);
                String syString = String.join("|", addressesSys);
                fileUtils.writeFile(syString+"\n",pathSy);
                addresses.clear();
                addressesSys.clear();
            }


        }
        System.out.println("路径"+pathSy+"生成文件数："+sy);
        log.info("路径"+pathSy+"生成文件数："+sy);

    }
    /**
     * 私钥L开头  1开头的是地址
     */
    @Override
    public void find1kaitou(String ad ,String add ,String path,int hao,int guilv) {
//        chushihua();
        int conutsum =0;
        String pathSy = path+"\\Sy"+hao+".txt";
        String pathgy = path+"\\gy"+hao+".txt";
        String gyPath = "D:\\btc\\miyao\\find.txt";
        FileUtils fileUtils = new FileUtils();
        fileUtils.createFile(pathSy);
        fileUtils.createFile(pathgy);
        int j = 0;
//        String add =1+"";

//        String ad = "67999218802305177194863145464757464115623021623585035533703579771544440055647";
        Set<String> addresses =  new HashSet<>();
        Set<String> addressesSys = new HashSet<>();
        BigInteger a = new BigInteger(ad);
//        BitcoinPrivateKeyGenerator1.BitcoinPrivateKey privateKey1 = BitcoinPrivateKeyGenerator1.generatePrivateKey();
        BigInteger sy = a;
        int flag=0;
        for(int k=0;k<guilv;k++) {
            sy = a.add(new BigInteger(add).multiply(BigInteger.valueOf(k*7)));
            for (int i = 0; sy.compareTo(new BigInteger("10000000"))> 0; i++) {
                flag = 0;
                //随机生成私钥

                if (i > 10000000) {
                    i = 0;
                    j++;
                }
//                BigInteger b = new BigInteger((i*10000)+"");

                BitcoinPrivateKeyGenerator1.BitcoinPrivateKey privateKey = BitcoinPrivateKeyGenerator1.generatePrivateKey(sy, new BigInteger(add));
                if (privateKey == null) {
                    flag = 1;
                }
                if (privateKey != null && BitcoinPrivateKeyGenerator1.isValidPrivateKey(privateKey.getPrivateKey())) {  //私钥正确的话

                    // 私钥转公钥 k
                    String wifPrivateKey = privateKey.getWIFCompressed();

                    KeyPair wifKeyPair = null;
                    try {
                        wifKeyPair = BitcoinKeyConverter.generateFromWIF(wifPrivateKey);
                    } catch (Exception e) {

                    }
                    if (wifKeyPair == null) {

                        sy = new BigInteger(privateKey.getHex(), 16).add(new BigInteger(add));
                        continue;
                    }
                    String address = wifKeyPair.getLegacyAddress(); // 比特币创始区块地址公钥地址
                    // 判断是否重复   不重复就加进来
                    if (panduanChongfu1(wifPrivateKey)) {
                        addresses.add(address);

                        addresses.add(wifKeyPair.getSegwitAddress());
                        addressesSys.add(wifPrivateKey);

                    ECKey ecKey = DumpedPrivateKey.fromBase58(MAINNET, wifPrivateKey).getKey();

                    // 判断是否与数据库中重复

                        // Bech32 3开头)
                        try {


                            Script p2wpkhScript = ScriptBuilder.createP2WPKHOutputScript(ecKey);
                            Script p2shScript = ScriptBuilder.createP2SHOutputScript(p2wpkhScript);
                            String p2sh = LegacyAddress.fromScriptHash(MAINNET, p2shScript.getPubKeyHash()).toString();  // 3开头
                            addresses.add(p2sh);


                        } catch (Exception e) {

                        }


                        NetworkParameters network = MainNetParams.get();
                        DumpedPrivateKey dumpedPrivateKey = DumpedPrivateKey.fromBase58(network, privateKey.getWIFUncompressed());
                        ECKey ecKey1 = dumpedPrivateKey.getKey();
                        // 生成地址
                        Address legacyAddress = LegacyAddress.fromKey(network, ecKey1);
                        addresses.add(legacyAddress.toString());
                        addressesSys.add(privateKey.getWIFUncompressed());
                    }

                    String publicKey = wifKeyPair.getPublicKeyCompressed();
                    publicKey = publicKey.substring(2);
                sy = clearHighestBit(sy);
                    sy = sy.add(new BigInteger(add));
                    if (sy.compareTo(new BigInteger("10000000")) < 0) {
                        flag = 1;
                    }

                }
//                System.out.println(privateKey);

                if (addresses.size() > 0 && (addresses.size() >= 300 || (k ==guilv-1&&flag==1) )) {
                    String addressString = String.join("|", addresses);
                    fileUtils.writeFile(addressString + "\n", pathgy);
                    String syString = String.join("|", addressesSys);
                    fileUtils.writeFile(syString + "\n", pathSy);
                    addresses.clear();
                    addressesSys.clear();
                }


            }
        }
        sy =  a.add(new BigInteger(add).multiply(BigInteger.valueOf(guilv*7)));
        System.out.println("路径"+pathSy+"生成文件数："+sy);
        log.info("路径"+pathSy+"生成文件数："+sy);

    }
    /**
     * 私钥L开头  1开头的是地址
     */
    @Override
    public void find1kaitou(String ad ,String add ,String path,int hao,int guilv,int jianfa) {
//        chushihua();
        int conutsum =0;
        String pathSy = path+"\\Sy"+hao+".txt";
        String pathgy = path+"\\gy"+hao+".txt";
        String gyPath = "D:\\btc\\miyao\\find.txt";
        FileUtils fileUtils = new FileUtils();
        fileUtils.createFile(pathSy);
        fileUtils.createFile(pathgy);
        int j = 0;
//        String add =1+"";

//        String ad = "67999218802305177194863145464757464115623021623585035533703579771544440055647";
        Set<String> addresses =  new HashSet<>();
        Set<String> addressesSys = new HashSet<>();
        BigInteger a = new BigInteger(ad);
//        BitcoinPrivateKeyGenerator1.BitcoinPrivateKey privateKey1 = BitcoinPrivateKeyGenerator1.generatePrivateKey();
        BigInteger sy = a;
        System.out.println("start:"+sy);
        int flag=0;
        for (int i = 0; i<1000000 ; i++){
            flag = 0;
            //随机生成私钥


            BitcoinPrivateKeyGenerator1.BitcoinPrivateKey privateKey = BitcoinPrivateKeyGenerator1.generatePrivateKey(sy,new BigInteger(add));
            if(privateKey==null){flag=1;}
            if (privateKey!=null && BitcoinPrivateKeyGenerator1.isValidPrivateKey(privateKey.getPrivateKey())) {  //私钥正确的话

                // 私钥转公钥 k
                String wifPrivateKey = privateKey.getWIFCompressed();

                KeyPair wifKeyPair = null;
                try {
                    wifKeyPair = BitcoinKeyConverter.generateFromWIF(wifPrivateKey);
                } catch (Exception e) {

                }
                if(wifKeyPair==null){

                    sy = new BigInteger(privateKey.getHex(),16).add(new BigInteger(add));
                    continue;
                }
                String address = wifKeyPair.getLegacyAddress(); // 比特币创始区块地址公钥地址
                // 判断是否重复   不重复就加进来
                if(GetSection.panduanSection(sy) && GetSection1.panduanSection(sy)) {
                    addresses.add(address);

                    addresses.add(wifKeyPair.getSegwitAddress());
                    addressesSys.add(wifPrivateKey);

                    ECKey ecKey = DumpedPrivateKey.fromBase58(MAINNET, wifPrivateKey).getKey();
                    // Bech32 3开头)
                    try {


                        Script p2wpkhScript = ScriptBuilder.createP2WPKHOutputScript(ecKey);
                        Script p2shScript = ScriptBuilder.createP2SHOutputScript(p2wpkhScript);
                        String p2sh = LegacyAddress.fromScriptHash(MAINNET, p2shScript.getPubKeyHash()).toString();  // 3开头
                        addresses.add(p2sh);


                    } catch (Exception e) {

                    }


                    NetworkParameters network = MainNetParams.get();
                    DumpedPrivateKey dumpedPrivateKey = DumpedPrivateKey.fromBase58(network, privateKey.getWIFUncompressed());
                    ECKey ecKey1 = dumpedPrivateKey.getKey();
                    // 判断是否与数据库中重复

                    // 生成地址
                    Address legacyAddress = LegacyAddress.fromKey(network, ecKey1);
                    addresses.add(legacyAddress.toString());
                    addressesSys.add(privateKey.getWIFUncompressed());
                }

                String publicKey = wifKeyPair.getPublicKeyCompressed();
                publicKey = publicKey.substring(2);
//                sy = clearHighestBit(sy);
                sy = sy.add(new BigInteger(add));
                if(sy.compareTo(new BigInteger("10000000"))<0){
                    flag = 1;
                }

            }else {
                sy = sy.add(new BigInteger(add));
            }
//                System.out.println(privateKey);

            if(addresses.size()>0 && addressesSys.size()>0 &&(addresses.size()>=300 || flag==1||i==999999 )) {
                String addressString = String.join("|", addresses);
                fileUtils.writeFile(addressString+"\n",pathgy);
                String syString = String.join("|", addressesSys);
                fileUtils.writeFile(syString+"\n",pathSy);
                addresses.clear();
                addressesSys.clear();
            }


        }
        System.out.println("路径"+pathSy+"生成文件数："+sy);
        log.info("路径"+pathSy+"生成文件数："+sy);

    }
    private void  chushihua(){
        findSyOne = syoneMapper.selectList(null);
    }

    private Boolean panduanChongfu(String privateKey){
//        LambdaQueryWrapper<Syone> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        lambdaQueryWrapper.likeRight(Syone::getSy, privateKey);
//        Boolean flag= syoneMapper.exists(lambdaQueryWrapper);
//
//        LambdaQueryWrapper<Sy> syLambdaQueryWrapper = new LambdaQueryWrapper<>();
//        syLambdaQueryWrapper.eq(Sy::getSy, privateKey);
//        Boolean flag1= syMapper.exists(syLambdaQueryWrapper);
//        if(!flag && !flag1){
//            Sy sy =new Sy();
//            sy.setSy(privateKey);
//            syMapper.insert(sy);
//            return true;
//        }
        return true;
    }

    private Boolean panduanChongfu1(String privateKey){
        LambdaQueryWrapper<Syone> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.likeRight(Syone::getSy, privateKey);
        Boolean flag= syoneMapper.exists(lambdaQueryWrapper);

        LambdaQueryWrapper<Sy> syLambdaQueryWrapper = new LambdaQueryWrapper<>();
        syLambdaQueryWrapper.eq(Sy::getSy, privateKey);
        Boolean flag1= syMapper.exists(syLambdaQueryWrapper);
        if(!flag && !flag1){
            Sy sy =new Sy();
            sy.setSy(privateKey);
            syMapper.insert(sy);
            return true;
        }
        return false;
    }
    @Override
    public void findBalance(int page) {
        // 查询数据
        LambdaQueryWrapper<Sy> lambdaQueryWrapper =new LambdaQueryWrapper();
        lambdaQueryWrapper.orderByDesc(Sy::getSy).last("LIMIT "+page*10000+", 100000");

        List<Sy> sys = syMapper.selectList(lambdaQueryWrapper);
//        sys.stream().forEach(System.out::println);
        sys.stream().forEach(

                s->{
                    GetManeyStrategy getManeyStrategy = new Blockcypher();
                    if(((s.getSy()).charAt(0)+"").equals("5")){

                    }  else {
                        KeyPair wifKeyPair = null;
                        try {
                            wifKeyPair = BitcoinKeyConverter.generateFromWIF(s.getSy());
                        } catch (Exception e) {

                        }
                        String address = wifKeyPair.getLegacyAddress();
                        String result = getManeyStrategy.getBalanceForAddresses(address);
                        JSONObject jsonResponse = new JSONObject(result);
                        Long maney = jsonResponse.getLong("final_balance");
                        if(maney>0){
                            System.out.println(s.getSy());
                        }
                        ECKey ecKey = DumpedPrivateKey.fromBase58(MAINNET, s.getSy()).getKey();
                        // Bech32 3开头)
                        try {


                            Script p2wpkhScript = ScriptBuilder.createP2WPKHOutputScript(ecKey);
                            Script p2shScript = ScriptBuilder.createP2SHOutputScript(p2wpkhScript);
                            String p2sh = LegacyAddress.fromScriptHash(MAINNET, p2shScript.getPubKeyHash()).toString();  // 3开头
                            String result1 = getManeyStrategy.getBalanceForAddresses(p2sh);
                            JSONObject jsonResponse1 = new JSONObject(result1);
                            Long maney1 = jsonResponse.getLong("final_balance");
                            if(maney1>0){
                                System.out.println("3开头："+s.getSy());
                            }

                        }catch (Exception e){

                        }


                    }


                });


        //        GetManeyStrategy getManeyStrategy = new Blockcypher();
//        String result = getManeyStrategy.getBalanceForAddresses(address);



    }

    @Override
    public void mysqlTomysql() throws Exception {
        List<Sy> syList = syMapper.selectList(null);
        String sy = "";
        for(int i=0;i<syList.size();i++){

                sy+=syList.get(i).getSy()+"|";

            if((i%100000==0 && i!=0)|| i+1==syList.size()){
                Syone syone = new Syone();
                syone.setSy(sy+"");
                syoneMapper.insert(syone);
                sy ="";
            }
        }


    }


    /**
     * 将最高位设置为0（清除最高位）
     * @param number 原始数字
     * @return 清除最高位后的结果
     */
    public static BigInteger clearHighestBit(BigInteger number) {
        if (number.equals(BigInteger.ZERO)) {
            return BigInteger.ZERO;
        }

        int bitLength = number.bitLength();
        if (bitLength == 0) {
            return BigInteger.ZERO;
        }

        // 创建掩码：除了最高位为0，其他位为1
        // 例如：对于 101010 (42)，bitLength=6，掩码为 011111 (31)
        BigInteger mask = BigInteger.ONE.shiftLeft(bitLength - 1).subtract(BigInteger.ONE);
        return number.and(mask);
    }
}
